﻿// Copyright (C) 2012 Kim Jensen, TaskJunction.com
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaskJunctionLib.Model;
using TaskJunctionLib.Mapping.JSON;
using TaskJunctionLib.Communication;
using System.Json;

namespace TaskJunctionLib.Repository
{
    public class TaskRepository : Repository<Task, TaskMapper>, ITaskRepository
    {
        protected override string GetResourceName()
        {
            return "tasks";
        }

        public IPagedList<Task> LoadListByProject(Project project, int page)
        {
            return LoadListByProject(project, page, null);
        }

        public IPagedList<Task> LoadListByProject(Project project, int page, TaskFilter taskFilter)
        {
            var map = CreateLoadListMap(page, taskFilter);
            map.Add("project_id", project.Id.ToString());
            return (IPagedList<Task>)_LoadList(GetResourceNameByValues(map));
        }

        public IPagedList<Task> LoadListByIteration(Iteration iteration, int page)
        {
            return LoadListByIteration(iteration, page, null);
        }

        public IPagedList<Task> LoadListByIteration(Iteration iteration, int page, TaskFilter taskFilter)
        {
            var map = CreateLoadListMap(page, taskFilter);
            map.Add("iteration_id", iteration.Id.ToString());
            return (IPagedList<Task>)_LoadList(GetResourceNameByValues(map));
        }

        private void mapTaskFilter(TaskFilter taskFilter, Dictionary<string, string> map)
        {
            const string task_filter = "task_filter";
            var statusIds = new int[5];
            var statuses = taskFilter.TaskStatusIds;
            for (int i = 0; i < statuses.Count; i++)
            {
                var statusId = (int)statuses[i];
                if (statusId > 0 && statusId <= statusIds.Length)
                    statusIds[statusId - 1] = 1;
            }
            for (int i = 0; i < statusIds.Length; i++)
            {
                map.Add(task_filter + "[task_status_" + (i + 1) + "]", statusIds[i].ToString());
            }
            if (!string.IsNullOrEmpty(taskFilter.Search))
                map.Add(task_filter + "[search]", taskFilter.Search);
            if (taskFilter.OwnerUserId != null)
                map.Add(task_filter + "[owner_user_id]", taskFilter.OwnerUserId.ToString());
        }

        private Dictionary<string, string> CreateLoadListMap(int page, TaskFilter taskFilter)
        {
            var map = new Dictionary<string, string>();
            map.Add("page", page.ToString());
            if (taskFilter != null)
            {
                mapTaskFilter(taskFilter, map);
            }
            return map;
        }


        public void Delete(IList<Task> tasks)
        {
            var ids = String.Join(",", tasks.Where(t => t.Id != null).Select(t => t.Id ?? 0).ToArray());
            var data = new JsonObject { { "ids", ids } };
            Client.Post(GetResourceName() + "/destroy", data.ToString());
        }
    }
}
